package com.gw.quotes.activity;

import net.tsz.afinal.annotation.view.ViewInject;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.gw.quotes.MyApplication;
import com.gw.quotes.R;
import com.gw.quotes.entity.system.User;
import com.gw.quotes.net.BaseServiceInter;
import com.gw.quotes.net.HttpCallBack;
import com.gw.quotes.net.Request;
import com.gw.quotes.net.Response;
import com.gw.quotes.net.service.UserLoginServiceImpl;
import com.gw.quotes.util.CommonTools;

public class LoginActivity extends BaseActivity {

	@ViewInject(id = R.id.back_img, click = "back")
	private ImageView back_img;
	@ViewInject(id = R.id.login_button, click = "onLogin")
	private Button login_button;
	@ViewInject(id = R.id.name_edit)
	private EditText name_edit;
	@ViewInject(id = R.id.pass_edit)
	private EditText pass_edit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
	}

	public void back(View v) {
		finish();
	}

	public void onLogin(View v) {
		dialog.show();
		BaseServiceInter httpService = new UserLoginServiceImpl();
		Request request = new Request();
		request.setLoginname(name_edit.getText().toString().trim());
		request.setPassword(pass_edit.getText().toString().trim());
		Response response = httpService.handler(request,
				new HttpCallBack<Response<User>>() {
					@Override
					public void onFinished(Response<User> response) {
						if (response.getResult() == Response.RESULT_SUCCESS) {
							// Toast.makeText(LoginActivity.this,response.getMsg(),Toast.LENGTH_SHORT).show();
							MyApplication.instance().setLoginUser(
									response.getObj());
							aCache.put("logininfo", name_edit.getText()
									.toString().trim()
									+ "||"
									+ pass_edit.getText().toString().trim());
							finish();
						} else {
							CommonTools.showShortToast(LoginActivity.this,
									response.getMsg());
							// Toast.makeText(LoginActivity.this,response.getMsg(),Toast.LENGTH_SHORT).show();
						}
						dialog.dismiss();
					}
				});
	}
}
